package com.moli.iov.base.remote.impl;

import com.alibaba.fastjson.JSONArray;
import com.moli.iov.base.constants.SysConstants;
import com.moli.iov.base.enums.FenceAlarmTypeEnum;
import com.moli.iov.base.enums.FenceShapeEnum;
import com.moli.iov.base.enums.YesOrNoEnum;
import com.moli.iov.base.model.dto.req.VehicleFenceBindingReqDto;
import com.moli.iov.base.model.dto.req.VehicleFenceBindingReqOpDto;
import com.moli.iov.base.model.dto.res.VehicleBaseBindingResDto;
import com.moli.iov.base.model.dto.res.VehicleFenceGpsResDto;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.model.po.VehicleFence;
import com.moli.iov.base.model.po.VehicleFenceBinding;
import com.moli.iov.base.remote.IVehicleFenceBindingRemoteService;
import com.moli.iov.base.service.IVehicleBaseService;
import com.moli.iov.base.service.IVehicleFenceBindingService;
import com.moli.iov.base.service.IVehicleFenceService;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.base.util.ServiceValidation;
import com.moli.iov.enums.UserTypeEnum;
import com.moli.iov.handle.alarm.model.dto.req.DisarmFenceAlarmReqDto;
import com.moli.iov.handle.alarm.service.IAlarmInfoTaskRequestService;
import com.moli.iov.his.model.dto.GpsDto;
import com.moli.iov.his.remote.HistoryRemoteService;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.GeoUtils;
import com.moli.iov.util.JsonUtils;
import com.moli.iov.util.ValidationUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;

/**
 * The type Vehicle fence binding remote service.
 *
 * @author wanghualin
 * @date 2019-09-21 09:27:48
 */
@Slf4j
@Service(group = "${service.group}")
@Transactional(rollbackFor = Exception.class)
public class IVehicleFenceBindingRemoteServiceImpl implements IVehicleFenceBindingRemoteService {

  @Autowired
  private IVehicleFenceBindingService iVehicleFenceBindingService;
  @Autowired
  private IVehicleFenceService iVehicleFenceService;
  @Autowired
  private IVehicleBaseService iVehicleBaseService;
  @Reference(group = "${service.group}", check = false,timeout = 10000)
  private HistoryRemoteService historyRemoteService;
  @Reference(group = "${service.group}", check = false,timeout = 10000)
  private IAlarmInfoTaskRequestService iAlarmInfoTaskRequestService;

  @Override
  public RestResponse saveVehicleFenceBinding(VehicleFenceBindingReqDto dto) {
    log.info("车辆监控-电子围栏-绑定-入参VehicleFenceBindingReqDto:{}" ,JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqDto.Save.class);
    //校验企业
    VehicleFence vehicleFence = iVehicleFenceService.getVehicleFence(dto.getFenceId());
    List<VehicleBase> vehicleBases = iVehicleBaseService.lisVehicleBaseByVin(dto.getListVin());
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.failure("绑定车辆不存在");
    }
    if (vehicleFence == null) {
      return   RestResponse.failure("绑定围栏不存在");
    }
    //添加校验数据
    List<VehicleFenceBinding> list = validateFenceBindingList(dto, vehicleFence, vehicleBases);
    if (CollectionUtils.isEmpty(list)) {
      return  RestResponse.failure("参数不合法");
    }
    RestResponse restResponse = iVehicleFenceBindingService
        .saveBatchVehicleFenceBinding(list, vehicleFence);
    //更新围栏绑定车辆个数
    if (restResponse.isFlag()) {
      int i = iVehicleFenceBindingService.countVehicleFenceBinding(vehicleFence.getId());
      vehicleFence.setTotal(i);
      iVehicleFenceService.updateVehicleFence(vehicleFence);
    }
    log.info("车辆监控-电子围栏-绑定-出参RestResponse:{}" ,JsonUtils.toJson(restResponse));
    return restResponse;
  }

  @Override
  public RestResponse deleteVehicleFenceBinding(VehicleFenceBindingReqDto dto) {
    log.info("车辆监控-电子围栏-解绑-入参VehicleFenceBindingReqDto:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqDto.Delete.class);
    VehicleFence vehicleFence = iVehicleFenceService.getVehicleFence(dto.getFenceId());
    if (vehicleFence == null) {
      return RestResponse.failure("绑定围栏不存在");
    }
    List<VehicleFenceBinding> list = iVehicleFenceBindingService
        .listVehicleFenceBinding(dto.getFenceId(), dto.getListVin());
    if (CollectionUtils.isEmpty(list)) {
      return RestResponse.failure("绑定车辆不存在");
    }
    Set<String>vinList=new HashSet<>();
    for (VehicleFenceBinding binding : list) {
      binding.setModifiedUser(dto.getSysUserId().toString());
      binding.setGmtModified(new Date());
      //删除
      binding.setIsDelete(YesOrNoEnum.YES.getCode());
      vinList.add(binding.getVin());
    }
    RestResponse restResponse = iVehicleFenceBindingService
        .deleteBatchVehicleFenceBinding(list, vehicleFence);
    //更新围栏绑定车辆个数
    if (restResponse.isFlag()) {
      int i = iVehicleFenceBindingService.countVehicleFenceBinding(vehicleFence.getId());
      vehicleFence.setTotal(i);
      iVehicleFenceService.updateVehicleFence(vehicleFence);
      if(!vinList.isEmpty()){
        for (String s : vinList) {
          DisarmFenceAlarmReqDto disarmFenceAlarmReqDto = new DisarmFenceAlarmReqDto();
          disarmFenceAlarmReqDto.setVin(s);
          disarmFenceAlarmReqDto.setFenceId(vehicleFence.getId());
          removeFenceAlarm(disarmFenceAlarmReqDto);
        }
      }
    }
    log.info("车辆监控-电子围栏-解绑-出参RestResponse:{}",JsonUtils.toJson(restResponse));
    return restResponse;
  }

  @Override
  public RestResponse listVehicleFenceBindingList(VehicleFenceBindingReqDto dto) {
    log.info("车辆监控 - 电子围栏-绑定围栏列表-入参VehicleFenceBindingReqDto:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqDto.Read.class);
    VehicleFence vehicleFence = iVehicleFenceService.getVehicleFence(dto.getFenceId());
    if (vehicleFence == null) {
      return RestResponse.failure("围栏不存在！");
    }
    dto.setCompanyId(vehicleFence.getCompanyId());
    List<VehicleBase> vehicleBases = new ArrayList<>();
    //得到已绑定车辆
    List<VehicleFenceBinding> list = iVehicleFenceBindingService
        .listVehicleFenceBinding(vehicleFence.getId(), null);
    if (!CollectionUtils.isEmpty(list)) {
      List<String> collect = list.stream().map(VehicleFenceBinding::getVin)
          .collect(Collectors.toList());
      dto.setListVin(collect);
    }
    vehicleBases = iVehicleBaseService
        .listVehicleFenceBindingList(dto);
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.success();
    }
    Stream<VehicleBase> limit = vehicleBases.stream().limit(10);
    List<VehicleBaseBindingResDto> resDtoList = new ArrayList<>();
    limit.forEach(vehicleBase -> {
      VehicleBaseBindingResDto vehicleBaseBindingResDto = new VehicleBaseBindingResDto();
      CommonUtils.copyProperties(vehicleBase, vehicleBaseBindingResDto);
      resDtoList.add(vehicleBaseBindingResDto);
    });
    return RestResponse.success(resDtoList);
  }

  @Override
  public RestResponse listVehicleFenceUnbindingList(VehicleFenceBindingReqDto dto) {
    log.info("车辆监控 - 电子围栏-解绑围栏列表-入参VehicleFenceBindingReqDto:{}",JsonUtils.toJson(dto) );
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqDto.Read.class);
    VehicleFence vehicleFence = iVehicleFenceService.getVehicleFence(dto.getFenceId());
    if (vehicleFence == null) {
      return RestResponse.failure("围栏不存在！");
    }
    //权限
    if (UserTypeEnum.COMPANY.getFlag() == dto.getSysUserType().intValue()) {
      if (dto.getSysCompanyId().intValue() != vehicleFence.getCompanyId()) {
        return RestResponse.failure("没有权限查看");
      }
    }
    List<VehicleFenceBinding> list = iVehicleFenceBindingService
        .listVehicleFenceBinding(vehicleFence.getId(), null);
    if (CollectionUtils.isEmpty(list)) {
      return RestResponse.success();
    }
    List<String> collect = list.stream().map(VehicleFenceBinding::getVin)
        .collect(Collectors.toList());
    dto.setListVin(collect);
    List<VehicleBase> vehicleBases = iVehicleBaseService.listVehicleFenceUnBindingList(dto);
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.success();
    }
    List<VehicleBaseBindingResDto> resDtoList = new ArrayList<>();
    vehicleBases.forEach(vehicleBase -> {
      VehicleBaseBindingResDto vehicleBaseBindingResDto = new VehicleBaseBindingResDto();
      CommonUtils.copyProperties(vehicleBase, vehicleBaseBindingResDto);
      resDtoList.add(vehicleBaseBindingResDto);
    });
    return RestResponse.success(resDtoList);
  }

  @Override
  public RestResponse listVehicleFenceDetailList(VehicleFenceBindingReqDto dto) {
    log.info("车辆监控 - 电子围栏-绑定详情-入参VehicleFenceBindingReqDto:{}" ,JsonUtils.toJson(dto));
    VehicleFence vehicleFence = iVehicleFenceService.getVehicleFence(dto.getFenceId());
    if (vehicleFence == null) {
      return RestResponse.failure("围栏不存在！");
    }
    //得到已绑定车辆
    List<VehicleFenceBinding> list = iVehicleFenceBindingService
        .listVehicleFenceBinding(vehicleFence.getId(), null);
    if (CollectionUtils.isEmpty(list)) {
      return RestResponse.success();
    }
    List<String> collect = list.stream().map(VehicleFenceBinding::getVin)
        .collect(Collectors.toList());
    List<VehicleBase> vehicleBases = iVehicleBaseService.lisVehicleBaseByVin(collect);
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.success();
    }
    List<VehicleBaseBindingResDto> resDtoList = new ArrayList<>();
    for (VehicleBase vehicleBase : vehicleBases) {
      VehicleBaseBindingResDto resDto = new VehicleBaseBindingResDto();
      CommonUtils.copyProperties(vehicleBase, resDto);
      resDtoList.add(resDto);
    }
    return RestResponse.success(resDtoList);
  }

  @Override
  public RestResponse saveVehicleFenceBindingOp(VehicleFenceBindingReqOpDto dto) {
    log.info("op-车辆监控-电子围栏-绑定-入参VehicleFenceBindingReqOpDto:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqOpDto.Save.class);
    VehicleFence vehicleFence = iVehicleFenceService.getVehicleFence(dto.getFenceId());
    List<VehicleBase> vehicleBases = iVehicleBaseService
        .listVehicleBaseInVinByCompanyId(dto.getCompanyId(), dto.getListVin());
    if (vehicleFence == null) {
      return  RestResponse.failure("绑定围栏不存在");
    }
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.failure("绑定车辆不存在");
    }
    List<VehicleFenceBinding> list1 = iVehicleFenceBindingService
        .listVehicleFenceBinding(vehicleFence.getId(), dto.getListVin());
    if (!CollectionUtils.isEmpty(list1)) {
      return   RestResponse.failure("已绑定");
    }
    //添加校验数据
    List<VehicleFenceBinding> list = validateFenceBindingOpList(dto, vehicleFence, vehicleBases);
    if (CollectionUtils.isEmpty(list)) {
      RestResponse.failure("参数不合法");
    }
    RestResponse restResponse = iVehicleFenceBindingService
        .saveBatchVehicleFenceBinding(list, vehicleFence);
    //更新围栏绑定车辆个数
    if (restResponse.isFlag()) {
      int i = iVehicleFenceBindingService.countVehicleFenceBinding(vehicleFence.getId());
      vehicleFence.setTotal(i);
      iVehicleFenceService.updateVehicleFence(vehicleFence);
    }
    log.info("op-车辆监控-电子围栏-绑定-出参RestResponse:{}",JsonUtils.toJson(restResponse));
    return restResponse;
  }

  @Override
  public RestResponse deleteVehicleFenceBindingOp(VehicleFenceBindingReqOpDto dto) {
    log.info("op-车辆监控-电子围栏-解绑-入参VehicleFenceBindingReqOpDto:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqOpDto.Delete.class);
    VehicleFence vehicleFence = iVehicleFenceService.getVehicleFence(dto.getFenceId());
    if (vehicleFence == null) {
      return  RestResponse.failure("绑定围栏不存在");
    }
    List<VehicleBase> vehicleBases = iVehicleBaseService
        .listVehicleBaseInVinByCompanyId(dto.getCompanyId(), dto.getListVin());
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.failure("车辆不存在");
    }
    List<VehicleFenceBinding> list = iVehicleFenceBindingService
        .listVehicleFenceBinding(dto.getFenceId(), dto.getListVin());
    if (CollectionUtils.isEmpty(list)) {
      return RestResponse.success();
    }
    Set<String>vinList=new HashSet<>();
    for (VehicleFenceBinding binding : list) {
      binding.setGmtModified(new Date());
      //删除
      binding.setIsDelete(YesOrNoEnum.YES.getCode());
      vinList.add(binding.getVin());
    }
    RestResponse restResponse = iVehicleFenceBindingService
        .deleteBatchVehicleFenceBinding(list, vehicleFence);
    //更新围栏绑定车辆个数
    if (restResponse.isFlag()) {
      int i = iVehicleFenceBindingService.countVehicleFenceBinding(vehicleFence.getId());
      vehicleFence.setTotal(i);
      iVehicleFenceService.updateVehicleFence(vehicleFence);
      if(!vinList.isEmpty()){
        for (String s : vinList) {
          DisarmFenceAlarmReqDto disarmFenceAlarmReqDto = new DisarmFenceAlarmReqDto();
          disarmFenceAlarmReqDto.setVin(s);
          disarmFenceAlarmReqDto.setFenceId(vehicleFence.getId());
          removeFenceAlarm(disarmFenceAlarmReqDto);
        }
      }
    }
    log.info("op-车辆监控-电子围栏-解绑-出参RestResponse:{}",JsonUtils.toJson(restResponse));
    return restResponse;
  }

  @Override
  public RestResponse deleteAllVehicleFenceBinding(VehicleFenceBindingReqOpDto dto) {
    log.info("op-车辆监控-电子围栏-解绑全部-入参RestResponse:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqOpDto.DeleteAll.class);
    List<VehicleBase> vehicleBases = iVehicleBaseService
        .listVehicleBaseInVinByCompanyId(dto.getCompanyId(), dto.getListVin());
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.failure("车辆不存在");
    }
    List<VehicleFenceBinding> list = iVehicleFenceBindingService
        .listVehicleFenceBinding(dto.getListVin());
    if (CollectionUtils.isEmpty(list)) {
      return RestResponse.success();
    }
    Set<Long> collect = list.stream().map(VehicleFenceBinding::getFenceId)
        .collect(Collectors.toSet());
    if (CollectionUtils.isEmpty(collect)) {
      return RestResponse.success();
    }
    List<VehicleFence> vehicleFences = iVehicleFenceService.listVehicleFenceByIds(collect);
    if (CollectionUtils.isEmpty(vehicleFences)) {
      return RestResponse.success();
    }
    List<VehicleFence> source = vehicleFences.stream().filter(
        vehicleFence -> vehicleFence.getCompanyId().longValue() == dto.getCompanyId().longValue()
            && vehicleFence.getAlarmType().intValue() == dto.getAlarmType().intValue())
        .collect(Collectors.toList());
    if (CollectionUtils.isEmpty(source)) {
      return RestResponse.success();
    }
    Map<Long, List<VehicleFenceBinding>> collect1 = list.stream()
        .collect(Collectors.groupingBy(VehicleFenceBinding::getFenceId));
    //修改
    source.forEach(vehicleFence -> {
      List<VehicleFenceBinding> delete = collect1.get(vehicleFence.getId());
      if (!CollectionUtils.isEmpty(delete)) {
        iVehicleFenceBindingService.deleteBatchVehicleFenceBinding(delete, vehicleFence);
        int i = iVehicleFenceBindingService.countVehicleFenceBinding(vehicleFence.getId());
        vehicleFence.setTotal(i);
        iVehicleFenceService.updateVehicleFence(vehicleFence);
        //解除围栏报警
        for (VehicleFenceBinding binding : delete) {
          DisarmFenceAlarmReqDto disarmFenceAlarmReqDto=new DisarmFenceAlarmReqDto();
          disarmFenceAlarmReqDto.setVin(binding.getVin());
          disarmFenceAlarmReqDto.setFenceId(binding.getFenceId());
          removeFenceAlarm(disarmFenceAlarmReqDto);
        }
      }
    });
    RestResponse success = RestResponse.success();
    log.info("op-车辆监控-电子围栏-解绑全部-出参RestResponse:{}",JsonUtils.toJson(success));
    return success;
  }

  @Override
  public RestResponse isVehicleInsideFence(VehicleFenceBindingReqOpDto dto) {
    log.info("op-车辆监控-电子围栏-是否在围栏内-入参VehicleFenceBindingReqOpDto:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqOpDto.Inside.class);
    List<String> vin = new ArrayList<>();
    vin.add(dto.getVin());
    List<VehicleBase> vehicleBases = iVehicleBaseService
        .listVehicleBaseInVinByCompanyId(dto.getCompanyId(), vin);
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.failure("车辆不存在");
    }
    VehicleFence vehicleFence = iVehicleFenceService.getVehicleFence(dto.getFenceId());
    if (vehicleFence==null) {
      return RestResponse.failure("围栏不存在");
    }

    RestResponse<List<GpsDto>> listRestResponse = historyRemoteService.listGps(dto.getVin());
    log.info(SysConstants.IOV_SERVICE_HIS+":"+listRestResponse);
    ServiceValidation.validationService(listRestResponse, SysConstants.IOV_SERVICE_HIS);
    if (CollectionUtils.isEmpty(listRestResponse.getData())){
      return  RestResponse.success(false);
    }
    //TODO 调用 服务
    //处理数据
    boolean b = handleData(vehicleFence, listRestResponse.getData());
    RestResponse<Boolean> success = RestResponse.success(b);
    log.info("op-车辆监控-电子围栏-是否在围栏内-出参VehicleFenceBindingReqOpDto:{}",JsonUtils.toJson(success));
    return success;
  }

  @Override
  public RestResponse isVehicleInsideFenceList(VehicleFenceBindingReqOpDto dto) {
    log.info("op-车辆监控-电子围栏-是否在围栏内-入参VehicleFenceBindingReqOpDto:{}",JsonUtils.toJson(dto));
    ValidationUtils.validateBean(dto, VehicleFenceBindingReqOpDto.InsideList.class);
    List<String> vin = new ArrayList<>();
    vin.add(dto.getVin());
    List<VehicleBase> vehicleBases = iVehicleBaseService
            .listVehicleBaseInVinByCompanyId(dto.getCompanyId(), vin);
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return RestResponse.failure("车辆不存在");
    }
    List<VehicleFence> vehicleFences = iVehicleFenceService.listVehicleFenceByIds(new HashSet<>(dto.getFenceIds()));
    if (CollectionUtils.isEmpty(vehicleFences)) {
      return RestResponse.failure("围栏不存在");
    }

    RestResponse<List<GpsDto>> listRestResponse = historyRemoteService.listGps(dto.getVin());
    log.info(SysConstants.IOV_SERVICE_HIS+":"+listRestResponse);
    ServiceValidation.validationService(listRestResponse, SysConstants.IOV_SERVICE_HIS);
    if (CollectionUtils.isEmpty(listRestResponse.getData())){
      return  RestResponse.success(-1);
    }
    //TODO 调用 服务
    Long fenceId= Long.valueOf(-1);
    //处理数据
    for (VehicleFence vehicleFence : vehicleFences) {
      boolean b = handleData(vehicleFence, listRestResponse.getData());
      if(b){
        fenceId=vehicleFence.getId();
        break;
      }
    }
    RestResponse success = RestResponse.success(fenceId);
    log.info("op-车辆监控-电子围栏-是否在围栏内-出参VehicleFenceBindingReqOpDto:{}",JsonUtils.toJson(success));
    return success;
  }

  /**
   * 判断点是否在围栏内vhe
   */
  private boolean handleData(VehicleFence vehicleFence, List<GpsDto> resDtos) {
    boolean flag = false;

    if (CollectionUtils.isEmpty(resDtos)) {
      return flag;
    }
    if (vehicleFence == null) {
      return flag;
    }
    if (vehicleFence.getFenceShape().intValue() == FenceShapeEnum.CIRCLE.getCode()) {
      VehicleFenceGpsResDto parse = JsonUtils
          .parse(vehicleFence.getFenceAreaCoordinate(), VehicleFenceGpsResDto.class);
      for (GpsDto resDto : resDtos) {
          BigDecimal a = BigDecimal.valueOf(vehicleFence.getRadius());
          BigDecimal b = BigDecimal.valueOf(1000);
        if (GeoUtils.isInCircle(a.divide(b,4,BigDecimal.ROUND_UP).doubleValue(), parse.getLatitude(), parse.getLongitude(),
            resDto.getLatitude(),
            resDto.getLongitude())) {
          flag = true;
          break;
        }
      }
    } else {
      List<VehicleFenceGpsResDto> dtoList = JSONArray
          .parseArray(vehicleFence.getFenceAreaCoordinateRange(), VehicleFenceGpsResDto.class);
      List<Map<String, Double>> dto = dtoList.stream().map(this::toMap)
          .collect(Collectors.toList());
      for (GpsDto resDto : resDtos) {
        boolean pointInPolygon = GeoUtils
            .isPointInPolygon(dto, resDto.getLongitude(), resDto.getLatitude());
        if (pointInPolygon){
          flag = true;
          break;
        }
      }
    }
    return flag;
  }

  private Map<String, Double> toMap(VehicleFenceGpsResDto dto) {
    Map<String, Double> map = new HashMap<>();
    map.put("longitude", dto.getLongitude());
    map.put("latitude", dto.getLatitude());
    return map;
  }


  /**
   * 绑定围栏  添加校验数据 op
   */
  public List<VehicleFenceBinding> validateFenceBindingOpList(VehicleFenceBindingReqOpDto dto,
      VehicleFence vehicleFence, List<VehicleBase> vehicleBases) {
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return null;
    }

    if (!CollectionUtils.isEmpty(dto) && !CollectionUtils.isEmpty(dto.getListVin())
        && !CollectionUtils.isEmpty(dto.getFenceId())) {
      Map<String, Long> collect = vehicleBases.stream()
          .collect(Collectors.toMap(VehicleBase::getVin, VehicleBase::getCompanyId));
      List<VehicleFenceBinding> list = new ArrayList<>();
      for (String vin : dto.getListVin()) {
        Long companyId = collect.get(vin);
        Date now = Calendar.getInstance().getTime();
        if (companyId == null || companyId.longValue() != vehicleFence.getCompanyId()) {
          continue;
        }
        //每台车辆只能绑定唯一的一个“出围栏报警”围栏
        if (vehicleFence.getAlarmType().intValue()== FenceAlarmTypeEnum.OUT.getCode()){
          List<VehicleFenceBinding> list1 = iVehicleFenceBindingService.listVehicleFenceBinding(vin, FenceAlarmTypeEnum.OUT.getCode());
          if (list!=null&&list.size()>0){
            continue;
          }
        }
        VehicleFenceBinding binding = new VehicleFenceBinding();
        binding.setCompanyId(vehicleFence.getCompanyId());
        binding.setCompanyName(vehicleFence.getCompanyName());
        binding.setGmtCreate(now);
        binding.setGmtModified(now);
        binding.setAlarmType(vehicleFence.getAlarmType());
        binding.setVin(vin);
        binding.setFenceId(vehicleFence.getId());
        binding.setBindingTime(now);
        binding.setSource(1);
        list.add(binding);
      }
      return list;
    }
    return null;
  }

  /**
   * 绑定围栏  添加校验数据
   */
  public List<VehicleFenceBinding> validateFenceBindingList(VehicleFenceBindingReqDto dto,
      VehicleFence vehicleFence, List<VehicleBase> vehicleBases) {
    if (CollectionUtils.isEmpty(vehicleBases)) {
      return null;
    }

    if (!CollectionUtils.isEmpty(dto) && !CollectionUtils.isEmpty(dto.getListVin())
        && !CollectionUtils.isEmpty(dto.getFenceId())) {
      Map<String, Long> collect = vehicleBases.stream()
          .collect(Collectors.toMap(VehicleBase::getVin, VehicleBase::getCompanyId));
      List<VehicleFenceBinding> list = new ArrayList<>();
      for (String vin : dto.getListVin()) {
        Long companyId = collect.get(vin);
        Date now = Calendar.getInstance().getTime();
        if (companyId == null || companyId.longValue() != vehicleFence.getCompanyId()) {
          continue;
        }
        //每台车辆只能绑定唯一的一个“出围栏报警”围栏
        if (vehicleFence.getAlarmType().intValue()== FenceAlarmTypeEnum.OUT.getCode()){
          List<VehicleFenceBinding> list1 = iVehicleFenceBindingService.listVehicleFenceBinding(vin, FenceAlarmTypeEnum.OUT.getCode());
            if (list!=null&&list.size()>0){
              continue;
            }
        }
        VehicleFenceBinding binding = new VehicleFenceBinding();
        binding.setCompanyId(vehicleFence.getCompanyId());
        binding.setCompanyName(vehicleFence.getCompanyName());
        binding.setGmtCreate(now);
        binding.setGmtModified(now);
        binding.setModifiedUser(dto.getSysUserId().toString());
        binding.setCreateUser(dto.getSysUserId().toString());
        binding.setVin(vin);
        binding.setFenceId(vehicleFence.getId());
        binding.setBindingTime(now);
        binding.setAlarmType(vehicleFence.getAlarmType());
        list.add(binding);
      }
      return list;
    }
    return null;
  }

  /**
   * 解除报警
   */
  @Async
  public  void removeFenceAlarm (DisarmFenceAlarmReqDto disarmFenceAlarmReqDto){
      try {
          RestResponse restResponse = iAlarmInfoTaskRequestService.disarmFenceAlarm(disarmFenceAlarmReqDto);
          log.info("解除-围栏-报警-出参：{}",JsonUtils.toJson(restResponse));
      }catch (Exception e){
          log.error("解除-围栏-报警异常 参数disarmFenceAlarmReqDto:{}",disarmFenceAlarmReqDto);
      }

  }
}
